package com.begonzales.locker.datadictionary;

import com.begonzales.locker.dto.HeaderDTO;
import com.begonzales.locker.dto.HeaderItemDTO;
import com.begonzales.locker.dto.ItemDTO;
import com.begonzales.locker.dto.MapperDomainTestCase;
import com.begonzales.locker.mapper.CTable;
import com.begonzales.locker.mapper.CTableColumns;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: BGONZ8
 * Date: 9/20/11
 * Time: 2:30 PM
 * To change this template use File | Settings | File Templates.
 */
public class AppTable {

    private CTable table = null;
    private CTableColumns columns = null;
    private HeaderItemDTO  headerItemDTO = null;
    private HeaderDTO headerDTO = null;
    private ItemDTO itemDTO = null;
    private MapperDomainTestCase mtc = null;
    private String name = "";

	public String tableName;
    public String tableClassMapperPath;
	public String tableClassPath;

    protected List<AppField> appFields;

    /**
     *
     */
    public AppTable()
    {
        appFields = new ArrayList<AppField>();
        table = new CTable();
        columns = new CTableColumns();
        headerItemDTO = new  HeaderItemDTO();
        headerDTO = new HeaderDTO();
        itemDTO = new ItemDTO();
        mtc = new MapperDomainTestCase();
        mtc.setItems(headerItemDTO.getItem());
        mtc.setDomainName("=0=");
        mtc.setName("=0=");
        mtc.setTable(table);
    }

    public void writeToFile(String fileName, String content)
    {
        try {
            File removeFile = new File(fileName);
            if ( removeFile.exists())
            {
                removeFile.delete();
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
            out.write( content );
            out.close();
        } catch (IOException e) {
        }
    }
    public String getMapperTestCase()
    {
        String retval = "";
        retval = mtc.toString();
        return retval;
    }
    public String getMapperXML()
    {
        String retval = "";
        retval = table.emitMapper() ;
        return retval;
    }
    public String getDomainEntity()
    {
        String retval = "";
        retval = table.getFileEntityJava(mtc.getDomainName()) ;
        return retval;
    }
    public String getDomainInterface()
    {
        String retval = "";
        retval = table.getFileInterfaceJava(mtc.getDomainName()) ;
        return retval;
    }
    public String getDtoFile()
    {
        String retval = "";
        retval = headerItemDTO.toString() ;
        return retval;
    }
    /**
     *
     * @param item
     * @return
     */
    public AppField add(AppField item)
    {
        appFields.add(item);
        bind(item);
        return item;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        mtc.setDomainName(name);
        mtc.setName(name);
        headerDTO.setHeaderName(name + "DTO");
        this.name = name;
    }

    /**
     *
     * @param item
     */
    public void bind(AppField item)
    {
        table.cols.add(new CTableColumns(item.getColumnFieldName()
                , item.getColumnFieldType()
                , item.getColumnMetaName()
                , item.getItemJavaType()
                , item.getPrimaryKey()));
        ItemDTO itm = headerItemDTO.add(new ItemDTO(item.getColumnMetaName()
                , item.getItemJavaType()
                , item.getItemAccessName()));
        itm.setPrimaryKey(item.getPrimaryKey());
    }

    /**
     *
     * @return
     */
    public CTable getTable() {
        return table;
    }

    /**
     *
     * @param table
     */
    public void setTable(CTable table) {
        this.table = table;
    }

    /**
     *
     * @return
     */
    public CTableColumns getColumns() {
        return columns;
    }

    public void setColumns(CTableColumns columns) {
        this.columns = columns;
    }

    public HeaderItemDTO getHeaderItemDTO() {
        return headerItemDTO;
    }

    public void setHeaderItemDTO(HeaderItemDTO headerItemDTO) {
        this.headerItemDTO = headerItemDTO;
    }


    public List<AppField> getAppFields() {
        if ( null == appFields )
        {
            appFields = new ArrayList<AppField>();
        }
        return appFields;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        table.tableName = tableName;
        this.tableName = tableName;
    }

    public String getTableClassMapperPath() {
        return tableClassMapperPath;
    }

    public void setTableClassMapperPath(String tableClassMapperPath) {
        table.tableClassMapperPath = tableClassMapperPath;
        this.tableClassMapperPath = tableClassMapperPath;
    }

    public String getTableClassPath() {
        return tableClassPath;
    }

    public void setTableClassPath(String tableClassPath) {
        table.tableClassPath = tableClassPath;
        this.tableClassPath = tableClassPath;
    }
}
